package com.cgsrp.cgsrp_community.service.impl;

import com.cgsrp.cgsrp_community.service.BlogService;
import com.cgsrp.cgsrp_user_province.dao.UserMapper;
import com.cgsrp.cgsrp_user_province.domain.User;
import com.cgsrp.utils.Result;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.cgsrp.cgsrp_community.dao.BlogFollowMapper;
import com.cgsrp.cgsrp_community.domain.BlogFollow;
import com.cgsrp.cgsrp_community.service.BlogFollowService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BlogFollowServiceImpl implements BlogFollowService{

    @Resource
    private BlogFollowMapper blogFollowMapper;

    @Resource
    private BlogService blogService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(Integer blogerId,Integer userId) {
        return blogFollowMapper.deleteByPrimaryKey(blogerId,userId);
    }

    @Override
    public int insert(BlogFollow record) {
        return blogFollowMapper.insert(record);
    }

    @Override
    public int insertSelective(BlogFollow record) {
        return blogFollowMapper.insertSelective(record);
    }

    @Override
    public BlogFollow selectByPrimaryKey(Integer blogerId,Integer userId) {
        return blogFollowMapper.selectByPrimaryKey(blogerId,userId);
    }

    @Override
    public int updateByPrimaryKeySelective(BlogFollow record) {
        return blogFollowMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(BlogFollow record) {
        return blogFollowMapper.updateByPrimaryKey(record);
    }

    @Override
    public Result getFollow(int userId) {
        List<Integer> list = blogFollowMapper.findFollowByUserId(userId);
        return Result.suc("查询成功",list);
    }

    @Override
    public Result nFollow(BlogFollow blogFollow) {
        blogFollow.setFollowTime(new Date());
        if (blogFollowMapper.deleteByPrimaryKey(blogFollow.getBlogerId(),blogFollow.getUserId())>0){
            redisTemplate.delete("blog");
            redisTemplate.delete("blog_follow");
            return Result.success("取消关注");
        }else {
            return Result.failure("系统出错");
        }
    }

    @Override
    public Result findFollowUsers(Integer id) {
        List<Integer> userIds = blogFollowMapper.findFollowByUserId(id);
        ArrayList<User> users = new ArrayList<>();
        for (Integer userId : userIds) {
            User user = userMapper.selectByPrimaryKey(userId);
            users.add(user);
        }
        return Result.suc("查询成功",users);
    }

    @Override
    public Result addFollow(BlogFollow blogFollow) {
        blogFollow.setFollowTime(new Date());
        if (blogFollowMapper.insert(blogFollow)>0){
            redisTemplate.delete("blog");
            redisTemplate.delete("blog_follow");
            return Result.success("关注成功");
        }else {
            return Result.failure("系统出错");
        }
    }

}
